package csc.fresher.view;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.commons.EncryptMD5;
import csc.fresher.entity.SystemUser;
import csc.fresher.service.SystemLoginService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemLoginManageBean implements Serializable {
	private static final long serialVersionUID = 3325780045480163859L;

	private SystemUser systemUser = null;

	private String userName;
	private String password;
	private String oldPassword;
	private String newPassword;
	private String confirmPassword;
	private String idCardNumber;
	private String firstName;
	private String lastName;
	private String midName;
	private String address;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getMidName() {
		return midName;
	}

	public void setMidName(String midName) {
		this.midName = midName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAddress2() {
		return address2;
	}

	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail2() {
		return email2;
	}

	public static List<Long> listRole;
	private List<Long> Roles;

	public List<Long> getRoles() {
		return Roles;
	}

	public void setRoles(List<Long> roles) {
		Roles = roles;
	}

	public void setEmail2(String email2) {
		this.email2 = email2;
	}

	public String getIdCardNumber() {
		return idCardNumber;
	}

	@Autowired
	private SystemUserService systemUserService;

	public static long userID;

	public String login() {
		FacesContext context = FacesContext.getCurrentInstance();

		if ("".endsWith(userName) || "".endsWith(password)) {
			//System.out.println(EncryptMD5.md5(password));
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Please enter User name & password!", null));
			System.out.println(EncryptMD5.md5(password));
		} else {
			systemUser = loginService.getSystemUser(userName, password);

			if (systemUser != null) {
				userID = systemUser.getSystemUserID();

				this.idCardNumber = systemUser.getIdCardNumber();
				this.firstName = systemUser.getFirstName();
				this.lastName = systemUser.getLastName();
				this.midName = systemUser.getMidName();
				this.address = systemUser.getAddress();
				this.address2 = systemUser.getAddress2();
				this.phone = systemUser.getPhoneNumber();
				this.phone2 = systemUser.getPhoneNumber2();
				this.email = systemUser.getEmail();
				this.email2 = systemUser.getEmail2();
				// context.getExternalContext().getSessionMap().put("userSS",
				// systemUser);
				beginTime = new Timestamp(System.currentTimeMillis());
				listRole = systemUserService.getRoleByUserName(userName);
				this.Roles = systemUserService.getRoleByUserName(userName);
				if (listRole != null) {
					for (int i = 0; i < listRole.size(); i++) {
						if (listRole.get(i) == 1 || listRole.get(i) == 2) {
							return "UserManagement";
						}
						if (listRole.get(i) == 3 || listRole.get(i) == 4) {
							return "/Group1/AccountManagement";							
						}
//						if (listRole.get(i) == 5) {
//							return null;
//						}
					}
				}
			} else {
				context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Your User name or Password is wrong, please try again!", null));								
			}
		}
		return "Logon";
	}

	public void setIdCardNumber(String idCardNumber) {
		this.idCardNumber = idCardNumber;

	}

	public List<String> getListLinks() {
		List<String> listLink = new ArrayList<String>();
		if (listRole != null) {
			for (int i = 0; i < listRole.size(); i++) {
				if (listRole.get(i) == 1 || listRole.get(i) == 2) {
					listLink.add("usermanager");
				}
				if (listRole.get(i) == 3 || listRole.get(i) == 4) {
					listLink.add("accountmanager");
				}
				if (listRole.get(i) == 5) {
					listLink.add("null");
				}
			}
		}

		return listLink;
	}

	private List<SelectItem> listLink = null;

	public void setListLink(List<SelectItem> listLink) {
		this.listLink = listLink;
	}

	public List<SelectItem> getListLink() {
		if ("".endsWith(userName) == false && "".endsWith(password) == false) {
			if (listLink == null) {
				listLink = new ArrayList<SelectItem>();
				Collection<String> links = getListLinks();
				for (String link : links) {
					listLink.add(new SelectItem(link.toString()));
				}
			}
		}
		return listLink;
	}

	public String getUserName() {

		return userName;

	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {

		return password;

	}

	public void setPassword(String password) {
		this.password = password;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public SystemLoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(SystemLoginService loginService) {
		this.loginService = loginService;
	}

	private String address2;
	private String phone;
	private String phone2;
	private String email;
	private String email2;

	public static Timestamp beginTime;

	@Autowired
	private SystemLoginService loginService;
	
	public void ResetUser(){
		this.idCardNumber = systemUser.getIdCardNumber();
		this.firstName = systemUser.getFirstName();
		this.lastName = systemUser.getLastName();
		this.midName = systemUser.getMidName();
		this.address = systemUser.getAddress();
		this.address2 = systemUser.getAddress2();
		this.phone = systemUser.getPhoneNumber();
		this.phone2 = systemUser.getPhoneNumber2();
		this.email = systemUser.getEmail();
		this.email2 = systemUser.getEmail2();
		
	}
	
	public void EditUser() {
		FacesContext context = FacesContext.getCurrentInstance();
		this.systemUser.setPassWord(EncryptMD5.md5(password));
		this.systemUser.setIdCardNumber(idCardNumber);
		this.systemUser.setFirstName(firstName);
		this.systemUser.setMidName(midName);
		this.systemUser.setLastName(lastName);
		this.systemUser.setAddress(address);
		this.systemUser.setAddress2(address2);
		this.systemUser.setPhoneNumber(phone);
		this.systemUser.setPhoneNumber2(phone2);
		this.systemUser.setEmail(email);
		this.systemUser.setEmail2(email2);
		this.systemUser
				.setLastUpdate(new Timestamp(System.currentTimeMillis()));
		systemUserService.updateSystemUser(systemUser);
		context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Update Success!", null));
	}

	public String CancelBtn() {
		ResetUser();
		return "AdminModify";
	}

	public String ChangePassword() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (systemUser.getPassWord().equals(EncryptMD5.md5(oldPassword))) {
			if (newPassword != null) {
				this.systemUser.setPassWord(EncryptMD5.md5(newPassword));
			}
			this.systemUser.setIdCardNumber(idCardNumber);
			this.systemUser.setFirstName(firstName);
			this.systemUser.setMidName(midName);
			this.systemUser.setLastName(lastName);
			this.systemUser.setAddress(address);
			this.systemUser.setAddress2(address2);
			this.systemUser.setPhoneNumber(phone);
			this.systemUser.setPhoneNumber2(phone2);
			this.systemUser.setEmail(email);
			this.systemUser.setEmail2(email2);
			this.systemUser.setLastUpdate(new Timestamp(System
					.currentTimeMillis()));
			systemUserService.updateSystemUser(systemUser);
			this.newPassword = "";
		}
		else
		{
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Wrong password!", null));
			return "ChangePassword";
		}
		return "UserManagement";
	}
}
